<?php
/**
 * Created by PhpStorm.
 * User: XZR
 * Date: 2018/8/1
 * Time: 15:21
 */

namespace Api\Controller;


//生成分享图片
use Api\Model\MedicalRecordModel;
use Api\Model\UserModel;
use Api\Model\UserVoteModel;
use Common\Controller\BaseController;
use Common\Util\EncryUtils;

class ImageController extends BaseController
{

    public function index(){
/*        $data=[
            'commend_status'=>0,
            'name'=>"程耀国 教授 北京大学第三医院",
            'id'=>'181126-001',
            'age'=>33,
            'sex'=>'男',
            'clear'=>'3年',
            'glass'=>'2.5年',
            'rate'=>20,
        ];
        $json = json_encode($data);*/
        //echo $json;

        $data = $this->getPostParam('data');
        $data = base64_decode(urldecode($data));
        $data= json_decode($data,true);
        $fire_count = ceil($data['new_comment_count']/2)>5?5: ceil($data['new_comment_count']/2);
        $image_path='./Public/fire'.$fire_count.".jpg";
        header("content-type:image/jpeg");
        $img = imagecreatefromjpeg($image_path);
/*        if($data['commend_status']){
            $img = imagecreatefromjpeg('./Public/commend.jpg');
        }else{
            $img = imagecreatefromjpeg('./Public/uncommend.jpg');
        }*/
        $font=dirname(dirname(dirname(__DIR__))) ."/Public/msyh.ttc";
        $black = imagecolorallocate($img, 0x06, 0x06, 0x06);  // 颜色
        $grey = imagecolorallocate($img, 149, 150, 152);  // 颜色

        imagettftext($img, 20, 0, 72, 87, $black, $font,$data['name'] );       // 写入name

        imagettftext($img, 17, 0, 95, 172, $grey, $font,$data['id'] );       // 写入id

        imagettftext($img, 15, 0, 519, 443, $black, $font,$data['new_comment_count'] );       // 写入评论数
        $remark = [];
        if (mb_strlen($data['remark'], 'utf-8') > 24){
            $i = 0;
            for($len = mb_strlen($data['remark']); $len > 0;){
                if ($i == 0){
                    $remark[] = mb_substr($data['remark'], (0 + 24 * $i), 24);  // 第一行24个中文
                }else{
                    $remark[] = mb_substr($data['remark'], (0 + 24 * $i), 28);  // 第二行及以后28个中文
                }
                $i++;
                $len -= 24;
            }
        }else{
            $remark[] = $data['remark'];
        }
        $line = 0;
        foreach ($remark as $k => $v){
            if ($line == 0){
                imagettftext($img, 17, 0, 130, 230, $grey, $font,$remark[$k]);       // 写入备注， 第一行
            }else{
                if ($line == 2){
                    if (mb_strlen($remark[$k], 'utf-8') == 28){
                        $remark[$k] = mb_substr($remark[$k], 0, 24) . '......';
                    }
                    imagettftext($img, 17, 0, 50, 230 + $line * 40, $grey, $font,$remark[$k]);
                    break;
                }
                imagettftext($img, 17, 0, 50, 230 + $line * 40, $grey, $font,$remark[$k]);       // 写入备注
            }
            $line++;
        }

        $bule = imagecolorallocate($img,0x27,0xbb,0xff);//创建一个颜色，以供使用
        imagettftext($img, 25, 0, 365, 538, $bule, $font,$data['rate']."%"  );       // 写入glass
        imagettftext($img, 25, 0, 366, 539, $bule, $font,$data['rate']."%"  );

        imagejpeg($img);     // 生成jpeg格式图片
        imagedestroy($img);  // 销毁图片*/
    }
    public function show(){
        $record_id =$this->getPostParam('record_id');
        $record = MedicalRecordModel::singleton()->find($record_id);
        $user =UserModel::singleton()->find($record['user_id']);
        $data =[
            'commend_status'=>$record['comment_count']>0?1:0,
            'name'=>$user['name']." ".$user['duty']." ".$user['hospital'],
            'id'=>$record['id_number'],
            'age'=>$record['age'],
            'sex'=>$record['sex'],
            'clear'=>$record['myopia_year'].'年',
            'remark' => $record['processing_results'],
            'rate'=>UserVoteModel::singleton()->getTotalRateByRecordId($record_id),
            'new_comment_count'=>$record['new_comment_count']
        ];
        $fire_count = ceil($record['new_comment_count']/2)>5?5: ceil($record['new_comment_count']/2);
        $image_path='./Public/fire'.$fire_count.".jpg";
        header("content-type:image/jpeg");
        $img = imagecreatefromjpeg($image_path);
/*        if($data['commend_status']){
            $img = imagecreatefromjpeg('./Public/commend.jpg');
        }else{
            $img = imagecreatefromjpeg('./Public/uncommend.jpg');
        }*/
        $font=dirname(dirname(dirname(__DIR__))) ."/Public/msyh.ttc";
        $black = imagecolorallocate($img, 0x06, 0x06, 0x06);  // 颜色
        $grey = imagecolorallocate($img, 149, 150, 152);  // 颜色

        imagettftext($img, 20, 0, 72, 87, $black, $font,$data['name'] );       // 写入name

        imagettftext($img, 17, 0, 95, 172, $grey, $font,$data['id'] );       // 写入id
        $remark = [];
        if (mb_strlen($data['remark'], 'utf-8') > 24){
            $i = 0;
            for($len = mb_strlen($data['remark']); $len > 0;){
                if ($i == 0){
                    $remark[] = mb_substr($data['remark'], (0 + 24 * $i), 24);  // 第一行24个中文
                }else{
                    $remark[] = mb_substr($data['remark'], (0 + 24 * $i), 28);  // 第二行及以后28个中文
                }
                $i++;
                $len -= 24;
            }
        }else{
            $remark[] = $data['remark'];
        }
        $line = 0;
        foreach ($remark as $k => $v){
            if ($line == 0){
                imagettftext($img, 17, 0, 130, 230, $grey, $font,$remark[$k]);       // 写入备注， 第一行
            }else{
                if ($line == 2){
                    if (mb_strlen($remark[$k], 'utf-8') == 28){
                        $remark[$k] = mb_substr($remark[$k], 0, 24) . '......';
                    }
                    imagettftext($img, 17, 0, 50, 230 + $line * 40, $grey, $font,$remark[$k]);
                    break;
                }
                imagettftext($img, 17, 0, 50, 230 + $line * 40, $grey, $font,$remark[$k]);       // 写入备注
            }
            $line++;
        }

        $bule = imagecolorallocate($img,0x27,0xbb,0xff);//创建一个颜色，以供使用
        imagettftext($img, 25, 0, 365, 538, $bule, $font,$data['rate']."%"  );       // 写入glass
        imagettftext($img, 25, 0, 366, 539, $bule, $font,$data['rate']."%"  );
        imagettftext($img, 15, 0, 519, 443, $black, $font,$data['new_comment_count'] );       // 写入评论数
        imagejpeg($img);     // 生成jpeg格式图片
        imagedestroy($img);  // 销毁图片*/
    }
}